<template>
    <el-card v-if="$auth.checkMenuPermissions('system:department:search')">
        <!--搜索组件-->
        <department-search :department-search="departmentSearchEntity" @onSearch="search"/>
        <!--按钮组件-->
        <department-option-btn @onOpenDialog="siwtchDialog" @ondepartmentReplicate="tableDataIsExpandAll" />
        <!-- 分隔符 -->
        <hr />
        <!--数据表-->
        <department-data :data="tableData" :data-title="departmentDataTitle" :update="tableDataUpdate"
            :is-expand-all="isExpandAll" :add="tableAdd" :del="tableDel" />
        <!--对话框-->
        <common-dialog ref="dialog" :childRef="dialogType === 'insert' ? 'departmentAddRef' : 'departmentUpdateRef'"
            :execute-confirm="dialogExecute" :width="720">
            <template v-slot:content>
                <department-add v-if="dialogType === 'insert'" ref="departmentAddRef" :department-add="departmentAddEntity"
                    :department-rules="addDepartmentEntityRles" :department-tree="addDepartmentTree" />
                <department-update v-else ref="departmentUpdateRef" :department-update="departmentUpdateEntity"
                    :department-rules="updateDepartmentEntityRles" :department-tree="updateDepartmentTree" />
            </template>
        </common-dialog>
    </el-card>
</template>

<script>
import departmentSearch from "./departmentSearch.vue";
import departmentOptionBtn from "./departmentOptionBtn.vue";
import departmentData from "./departmentData.vue";
import commonDialog from "@/components/common/commonDialog.vue";
import departmentAdd from "./departmentAdd.vue";
import departmentUpdate from "./departmentUpdate.vue";
import {
    departmentSearchEntity,
    departmentAddEntity,
    departmentUpdateEntity,
    departmentDataTitle,
} from "./entity/departmentEntity";
import { addDepartmentEntityRles, updateDepartmentEntityRles } from './entity/departmentverify'
import { getDepartmentTree, addDepartment, deparmtentInfo, updateDepartment, deleteDepartment } from "@/api/system/departmentApi";


export default {
    components: { departmentSearch, departmentOptionBtn, departmentData, commonDialog, departmentAdd, departmentUpdate },
    data() {
        return {
            departmentSearchEntity,
            departmentDataTitle,
            tableData: [],
            isExpandAll: true,
            dialogType: "insert",
            addDepartmentTree: [],
            updateDepartmentTree: [],
            departmentAddEntity,
            addDepartmentEntityRles,
            departmentUpdateEntity,
            updateDepartmentEntityRles
        };
    },
    methods: {
        //加载部门树数据
        loadDepartment() {
            getDepartmentTree()
                .then((data) => {
                    this.tableData = data;
                    //初始化新增使用的树
                    this.addDepartmentTree = data;
                    this.removeTreeNodeNullChildFields(this.addDepartmentTree)
                    //初始化修改使用的树
                    this.updateDepartmentTree = [this.$auth.getLoginInfo().companyTreeVO];
                })
                .catch((err) => {
                    this.$message.error(err);
                });
        },
        //搜索
        search() {
            getDepartmentTree(this.departmentSearchEntity)
                .then((data) => {
                    this.tableData = data;
                })
                .catch((err) => {
                    this.$message.error(err);
                });
        },
        //控制是否开启新增对话框
        siwtchDialog() {
            this.dialogType = "insert";
            this.$refs.dialog.switchEvent();
        },
        //控制是否打开修改对话框
        tableDataUpdate(id) {
            deparmtentInfo(id).then(data => {
                if (data.parentId === 0) {
                    data.parentId = data.companyId;
                }
                this.departmentUpdateEntity = data;
            }).catch(err => {
                this.$message.error(err);
            })
            this.dialogType = "update";
            this.$refs.dialog.switchEvent();
        },
        //表格执行新增
        tableAdd(data) {
            this.departmentAddEntity.parentId = data.companyId;
            this.dialogType = "insert";
            this.$refs.dialog.switchEvent();
        },
        //表格执行删除
        tableDel(data) {
            this.$confirm(`确认是否要删除 [${data.companyName}]?`, '操作确认', {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                await deleteDepartment(data.companyId).then(success => {
                    this.$message.success("删除成功");
                }).catch(err => {
                    this.$message.error(err);
                })
                //重新载入页面数据
                this.loadDepartment();
            }).catch(() => { })

        },
        //对话框执行
        async dialogExecute(event) {
            let result = this.$refs[event].validate();
            if (result) {
                if (event === 'departmentAddRef') {
                    let executeResult = await addDepartment(this.departmentAddEntity).then(data => {
                        this.$message.success("新增成功");
                        //重新载入页面数据
                        this.loadDepartment();
                        return true;
                    }).catch(err => {
                        this.$message.error(err);
                        return false;
                    })
                    return executeResult;
                } else {
                    let executeResult = await updateDepartment(this.departmentUpdateEntity).then(data => {
                        this.$message.success("修改成功");
                        //重新载入页面数据
                        this.loadDepartment();
                        return true;
                    }).catch(err => {
                        this.$message.error(err);
                        return false;
                    })
                    return executeResult;
                }
            } else {
                return false;
            }
        },
        //将激活对话框中的空子节点进行移除,防止组件渲染存在空子列
        removeTreeNodeNullChildFields(tree) {
            tree.map(t => {
                if (t.child && t.child.length > 0) {
                    this.removeTreeNodeNullChildFields(t.child);
                } else {
                    delete t.child;
                }
            })
        },
        //数据是否折叠
        tableDataIsExpandAll() {
            this.isExpandAll = !this.isExpandAll;
        }
    },
    mounted() {
        this.loadDepartment();
    },
};
</script>

<style scoped lang="scss"></style>
